﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace OTPlus
{
    public partial class Thread : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request.QueryString["activityid"] != null && Session["access_token"] != null)
            {
                //we have the basic info, but no page number.
                Int32 pagenumber = 0;

                if (Request.QueryString["page"] != null)
                {
                    Int32.TryParse(Request.QueryString["page"], out pagenumber);
                    //page numbers come in as "2" for what's really page 1, 
                    //and "1" for what's really zero
                    pagenumber = pagenumber - 1;
                }

                this.loadthread(Request.QueryString["activityid"], Session["access_token"].ToString(), pagenumber);
                

            }
        }
        
        private void loadthread(String activityid, String access_token, Int32 pagenumber)
        {
            Core.DataTransfer.ActivitySelect query = new Core.DataTransfer.ActivitySelect();
            query.AccessToken = access_token;
            query.ActivityID = activityid;
            
            query.PaginationRequest = new Core.DataTransfer.PaginationRequest();
            query.PaginationRequest.ItemsPerPage = Int32.Parse(System.Configuration.ConfigurationManager.AppSettings["commentsperpage"]);
            query.PaginationRequest.PaginationToken = (pagenumber * query.PaginationRequest.ItemsPerPage).ToString(); 
            
            Core.DataTransfer.ActivityResponse response = Core.Content.Activity.RetrieveComments(query);

            this.lblTitle.Text = response.Activity.TitleShort;

            this.repeaterComments.DataSource = response.Activity.Comments;
            this.repeaterComments.DataBind();

            this.loadMenu(response, access_token);
        }

        private void loadMenu(Core.DataTransfer.ActivityResponse activityResponse, String accessToken)
        { 
            MenuItem miTop;
            MenuItem miBottom;
            Int32 menuText = 1;

            for (int i = 0; i < getTotalPages(activityResponse); i++)
            {
                
                miTop = new MenuItem();
                miTop.Value = menuText.ToString();
                miTop.Text = menuText.ToString();
                miTop.NavigateUrl = "thread.aspx?activityid=" + activityResponse.Activity.ID
                    + "&page=" + menuText.ToString();

                this.menuPagesTop.Items.Add(miTop);


                miBottom = new MenuItem();
                miBottom.Value = menuText.ToString();
                miBottom.Text = menuText.ToString();
                miBottom.NavigateUrl = "thread.aspx?activityid=" + activityResponse.Activity.ID
                    + "&page=" + menuText.ToString();

                this.menuPagesBottom.Items.Add(miBottom);
                                
                menuText = menuText + 1;
            }

            //and after all that, maybe we won't even display anything. micro-optimize by firguring this out in advance i suppose
            if (menuPagesBottom.Items.Count <= 1)
            { this.menuPagesBottom.Visible = false; }

            if (menuPagesTop.Items.Count <= 1)
            { this.menuPagesTop.Visible = false; }

        }

        private Int32 getTotalPages(Core.DataTransfer.ActivityResponse activityResponse)
        { 
            long remainder;
            long quotient;
            Int32 pages;

            try { quotient = Math.DivRem(activityResponse.Activity.Replies,Int32.Parse(System.Configuration.ConfigurationManager.AppSettings["commentsperpage"]), out remainder); }
            catch
            { throw new Exception("Unable to calculate pages; division error."); }

            try { pages = Convert.ToInt32(quotient); }
            catch
            { throw new Exception("Unbale to convert long to integer while calculating pages."); }
            

            if (remainder != 0)
            { pages = pages + 1; }

            return pages;
        }
    }
}